/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author paulocanedo
 */
public class BallonTipPanel extends JPanel {

    public static enum Position {

        TOP, LEFT, BOTTOM, RIGHT
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        Position pos = Position.TOP;
        int hArrowSize = 30, vArrowSize = 20;
        int x = 2, y = 2, width = getWidth() - 4, height = getHeight() - 4, roundCorner = 20, gap = width - roundCorner - hArrowSize - 40;

        g2.setColor(Color.BLACK);
        Shape rrect = null;
        Shape polygon = null;

        switch (pos) {
            case TOP: {
                rrect = new RoundRectangle2D.Double(x, y + vArrowSize, width, height - vArrowSize, roundCorner, roundCorner);

                int x1 = roundCorner + gap, x2 = x1 + (hArrowSize / 2), x3 = x1 + hArrowSize;
                int y1 = y, y2 = y + vArrowSize;
                polygon = new Polygon(new int[]{x1, x3, x3}, new int[]{y2, y1, y2}, 3);
                break;
            }
            case LEFT: {
                break;
            }
            case BOTTOM: {
                break;
            }
            case RIGHT: {
                break;
            }
        }

        Area area = new Area(rrect);
        area.exclusiveOr(new Area(polygon));
        
        g2.setColor(new Color(154, 180, 232, 200));
        g2.fill(area);
        
        g2.setColor(Color.DARK_GRAY);
        g2.setStroke(new BasicStroke(2f));
        g2.draw(area);

        g2.dispose();
    }

    public static void main(String... args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new BallonTipPanel());

        frame.setBounds(0, 0, 400, 260);
        frame.setVisible(true);
    }
}
